require('./filter');
var _ = require('lodash');
var service = require('./service');
var Router = require('koa-router');
var router = module.exports = new Router({
  prefix: '/integral'
});

// 积分商城
router.get('/', loginRequired, function * (next) {
  var data = yield {
    goods: service.listGoods(this, 1, 10),
    integral: service.getIntegral(this, this.session.member.memberId),
  };

  this.body = this.render('templates/home.html', data);
});

// 积分商品
router.get('/goods', loginRequired, function * (next) {
  var data = yield service.listGoods(this);
  this.body = this.render('templates/goods.html', data);
});

// 流量包
router.get('/packages', loginRequired, function * (next) {
  var data = yield service.listPackage(this, this.session.member.memberMobile, 1, 100);
  this.body = this.render('templates/packages.html', data, {
    mobile: this.session.member.memberMobile
  });
});

// 千丁券
router.get('/coupons', loginRequired, function * (next) {
  var data = yield service.listCoupons(this);
  this.body = this.render('templates/coupons.html', data);
});

// 兑换说明
router.get('/instruction', function * (next) {
  this.body = this.render('templates/instruction.html');
});

// 商品详情
router.get('/details/:id', loginRequired, function * (next) {
  var data = yield service.details(this, this.params.id, this.session.member.memberId);
  this.body = this.render('templates/details.html', data);
});

// 兑换列表
router.get('/orders', loginRequired, function * (next) {
  var data = yield service.listOrder(this, this.session.member.memberId);
  this.body = this.render('templates/orders.html', data);
});

// 兑换详情
router.get('/order/:id', loginRequired, function * (next) {
  var order = yield service.getOrder(this, this.params.id);
  this.body = this.render('templates/order.html', {
    order: order.entity
  });
});

// 确认兑换
router.get('/confirm/:id', loginRequired, function * (next) {
  this.session['jf:goods'] = this.params.id;
  var data = yield {
    goods: service.details(this, this.params.id, this.session.member.memberId),
    address: this.session['ng:address'] || service.defaultAddress(this, this.session.member.memberId),
  };
  this.body = this.render('templates/confirm.html', data);
});

// 兑换成功
router.get('/success/:id', loginRequired, function * (next) {
  var data = yield service.getOrder(this, this.params.id);
  this.body = this.render('templates/success.html', data);
});

// 积分说明
router.get('/protocol', function * (next) {
  this.body = this.render('templates/protocol.html');
});

// 确认订单 - 列举收货人
router.get('/addresses', loginRequired, function * (next) {
  var data = yield service.listAddress(this, this.session.member.memberId);
  this.body = this.render('templates/addresses.html', data);
});

// 确认订单 - 选择收货人
router.get('/address/choose/:id', loginRequired, function * (next) {
  this.session['ng:address'] = yield service.getAddress(this, this.session.member.memberId, this.params.id);
  this.redirect('/integral/confirm/' + this.session['jf:goods']);
});

// 确认订单 - 新建收货人
router.get('/address', loginRequired, function * (next) {
  // 记录来源
  var address = {
    name: this.session.member.memberName,
    gender: this.session.member.memberGender,
    mobile: this.session.member.memberMobile
  };

  this.body = this.render('templates/address.html', address);
});

// 确认订单 - 新建收货人
router.post('/address', loginRequired, function * (next) {
  var project = this.session.projects[0];
  var self = this;
  var address = _.merge(this.request.body, {
    gender: this.request.body.gender || 0,
  }, {
    projectId: project.id,
    projectName: project.name,
    cityId: project.cityId,
    cityName: project.cityName,
    memberId: this.session.member.memberId,
    addressBusinessType: 0,
  });

  yield service.saveOrUpdate(this, address).then(function (data) {
    self.session['ng:address'] = data.entity;
    self.redirect('/integral/confirm/' + self.session['jf:goods']);
  }, function (data) {
    self.body = self.render('templates/address.html', address, {
      error: data.message
    });
  });
});

// // // // // // // // // // // // // // // // // // // // // //

// 热门兑换
router.get('/ajax/goods', loginRequired, function * (next) {
  var data = yield service.listGoods(this, this.query.page, this.query.size);
  this.body = this.render(200, data);
});

// 兑换优惠券
router.get('/ajax/coupons', loginRequired, function * (next) {
  var data = yield service.listCoupons(this, this.query.page, this.query.size);
  this.body = this.render(200, data);
});

// 流量包
router.get('/ajax/packages', loginRequired, function * (next) {
  var data = yield service.listPackage(this, this.query.mobile, this.query.page || 1, this.query.size || 100);
  this.body = this.render(200, data);
});

// 兑换列表
router.get('/ajax/orders', loginRequired, function * (next) {
  var data = yield service.listOrder(this, this.session.member.memberId, this.query.page, this.query.size);
  this.body = this.render(200, data);
});

// 追加数据
function appendMeta(form, session) {
  var project = session.projects[0];
  var data = _.clone(form);
  data.regionId = project.cityId;
  data.regionName = project.cityName;
  data.projectId = project.id;
  data.projectName = project.name;
  data.mid = session.member.memberId;
  data.memberName = session.member.memberName || '千丁用户';
  data.memberMobile = session.member.memberMobile;
  return data;
}

// 流量包下单
router.post('/ajax/placeorder', loginRequired, function * (next) {
  var self = this;
  var form = this.request.body;
  yield service.placeorder(this, appendMeta(form, this.session)).then(function (data) {
    self.body = self.render(200, data);
  }, function (data) {
    self.body = self.render(500, data);
  });
});

// 积分列表
router.get('/records', loginRequired, function * (next) {
  var pars = {
    memberId: this.session.member.memberId,
    pageNo: 1,
    pageSize: 20
  }
  var data = yield {
    details: service.pointsDetail(this, pars),
    inputs: service.pointsRevenueDetail(this, pars),
    outputs: service.pointsExpendDetail(this, pars)
  };
  this.body = this.render('templates/records.html', data);
});

// 更多明细
router.get('/ajax/details', loginRequired, function * () {
  var pars = {
    memberId: this.session.member.memberId,
    pageNo: this.query.page,
    pageSize: this.query.size
  }
  var data = yield service.pointsDetail(this, pars);
  this.body = this.template.render(200, data);
});

// 更多收入
router.get('/ajax/records', loginRequired, function * () {
  var pars = {
    memberId: this.session.member.memberId,
    pageNo: this.query.page,
    pageSize: this.query.size
  }
  var data = yield service.pointsRevenueDetail(this, pars);
  this.body = this.template.render(200, data);
});

// 更多支出
router.get('/ajax/deposit', loginRequired, function * () {
  var pars = {
    memberId: this.session.member.memberId,
    pageNo: this.query.page,
    pageSize: this.query.size
  }
  var data = yield service.pointsExpendDetail(this, pars);
  this.body = this.template.render(200, data);
});